package main_package;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

import part_2.GA_TSP_alg;

public class Main 
{

	public static void main(String[] args)
	{
		//initializing variables
		String str;
		FileReader fr = null;
		BufferedReader br = null;
		GA_TSP_alg gata = new GA_TSP_alg();

		try 
		{
			//reading the input for the tsp input
			fr = new FileReader("tsp.txt");
			br = new BufferedReader(fr);

			ArrayList<int[]> cities_locs=new ArrayList<int[]>();
			int [] city=new int[2];
			// as long as the file has more lines to read
			while ((str = br.readLine()) != null)
			{

				str=str.trim();

				//getting the coordinations of the city in the current line
				city=ourSplit(str);

				//adding the city to array of cities
				cities_locs.add(city);	

			}

			fr.close();
			br.close();

			//reading the best solution for the TSP problem
			fr = new FileReader("tsp-best.txt");
			br = new BufferedReader(fr);

			ArrayList<Integer> best=new ArrayList<Integer>();

			// as long as the file has more lines to read
			while ((str = br.readLine()) != null)
			{
				str=str.trim();
				best.add(Integer.parseInt(str));			
			}

			fr.close();
			br.close();

			//printing the date of beginning of the algorithm
			System.out.println(new java.util.Date());

			//running GA for TSP problem
			gata.run_alg(cities_locs,best);


			//printing date of finish
			System.out.println(new java.util.Date());

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/*
	 * input- str that represents a line which includes two coordinates(seperated by space)
	 */
	private static int[] ourSplit(String str)
	{
		//getting index of first space
		int i=str.indexOf(' ');
		int[] city=new int[2];

		//getting first coordinate
		city[0]=Integer.parseInt(str.substring(0, i));

		//skipping spaces
		while(i<str.length() && str.charAt(i)==' ')
			i++;

		//getting second coordinate
		city[1]=Integer.parseInt(str.substring(i,str.length()));
		return city;
	}
}
